package com.jhgk.jhcee.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgk.jhcee.common.exception.ParamException;
import com.jhgk.jhcee.data.auth.entity.SysRoleResourceRelation;
import com.jhgk.jhcee.data.auth.mapper.SysRoleResourceRelationMapper;
import com.jhgk.jhcee.system.service.SysRoleResourceRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: lemon_xzp
 * @Date: 2021/10/5 15:06
 */
@Service(value = "SysRoleResourceRelationServiceImpl")
public class SysRoleResourceRelationServiceImpl extends ServiceImpl<SysRoleResourceRelationMapper, SysRoleResourceRelation> implements SysRoleResourceRelationService {

    @Autowired
    SysRoleResourceRelationMapper sysRoleResourceRelationMapper;

    @Override
    public Boolean deleteRoleResourceRelation(Long roleId, List<Long> resourceIds) {
        if (null == roleId || resourceIds.isEmpty()) {
            throw new ParamException("删除角色资源关系 >>> 参数传入为空");
        }
        return sysRoleResourceRelationMapper.deleteByParamList(roleId, resourceIds);
    }

    @Override
    public Boolean deleteRoleResourceRelation(Long roleId, Long resourceId) {
        if (null == roleId || null == resourceId) {
            throw new ParamException("删除角色资源关系 >>> 参数传入为空");
        }
        return sysRoleResourceRelationMapper.deleteByParamOne(roleId, resourceId);
    }

    @Override
    public Boolean saveRoleResourceRelation(Long roleId, Long resourceId) {
        if (null == roleId || null == resourceId) {
            throw new ParamException("保存角色资源关系 >>> 参数传入为空");
        }
        QueryWrapper<SysRoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        wrapper.eq("resource_id", resourceId);
        SysRoleResourceRelation sysRoleResourceRelation = sysRoleResourceRelationMapper.selectOne(wrapper);
        if (null != sysRoleResourceRelation) {
            this.deleteRoleResourceRelation(roleId, resourceId);
        }
        sysRoleResourceRelation = new SysRoleResourceRelation();
        sysRoleResourceRelation.setRoleId(roleId);
        sysRoleResourceRelation.setResourceId(resourceId);
        return this.save(sysRoleResourceRelation);
    }

    @Override
    public Boolean saveRoleResourceRelation(Long roleId, List<Long> resourceIds) {
        if (null == roleId || resourceIds.isEmpty()) {
            throw new ParamException("保存角色资源关系 >>> 参数传入为空");
        }
        // 直接删除角色资源关系
        QueryWrapper<SysRoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        sysRoleResourceRelationMapper.delete(wrapper);
        Integer integer = sysRoleResourceRelationMapper.addRoleResourceRelation(roleId, resourceIds);
        return integer > 0;
    }

    @Override
    public Boolean saveRoleResourceRelation(List<Long> roleId, Long resourceId) {
        if (roleId.isEmpty() || null == resourceId) {
            throw new ParamException("保存角色资源关系 >>> 参数传入为空");
        }
        // 删除当前资源关联角色关系
        QueryWrapper<SysRoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("resource_id", resourceId);
        sysRoleResourceRelationMapper.delete(wrapper);
        Integer integer = sysRoleResourceRelationMapper.addRolesResource(roleId, resourceId);
        return integer > 0;
    }

    @Override
    public Boolean deleteRolesResource(List<Long> roleIds, Long resourceId) {
        if (roleIds.isEmpty() || null == resourceId) {
            throw new ParamException("删除角色资源关系 >>> 参数传入为空");
        }
        sysRoleResourceRelationMapper.deleteRolesResource(roleIds, resourceId);
        return null;
    }

    @Override
    public Boolean deleteResourceRelation(Long resourceId) {
        if (null == resourceId) {
            throw new ParamException("删除角色资源关系 >>> 参数传入为空");
        }
        QueryWrapper<SysRoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("resource_id", resourceId);
        int delete = sysRoleResourceRelationMapper.delete(wrapper);
        return delete > 0;
    }
}
